package com.fingard.dsp.bank.directbank.vbao01;

import java.net.URLDecoder;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.ICloseOrder;
import com.fingard.dsp.bank.reqrespobj.closeorder.CloseOrderState;
import com.fingard.dsp.bank.reqrespobj.closeorder.ReqCloseOrder;
import com.fingard.dsp.bank.reqrespobj.closeorder.RetAtsCloseOrder;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.vbao.service.trade.VbaoChargeSystemHandler;
import com.vbao.service.trade.bean.request.OneQRCodeInvalidRequest;
import com.vbao.service.trade.bean.request.TradeRequestXml;
import com.vbao.service.trade.utils.TradeCodeEnum;

public class VBAO01CloseOrder extends VBAO01Base implements ICloseOrder {
	@Override
	public RetAtsCloseOrder closeOrder(ReqCloseOrder p_req) {
		RetAtsCloseOrder retAts = new RetAtsCloseOrder(p_req.reqHeader);
		retAts.rdSeq = p_req.rdSeq;

		try {
			ActSetItem actItem = getBankConfig().getActSetItemByAny(p_req.ownAct, p_req.merchantId,	p_req.appId, "unifiedorder");
			if (actItem != null) {
				String customerNo = p_req.merchantId;
				if (StringHelper.isNullOrEmpty(customerNo)) {
					customerNo = actItem.merchantId;
				}
				
				// 统一关单业务参数
				OneQRCodeInvalidRequest oneQRPayCloseRequest = new OneQRCodeInvalidRequest();
				
				oneQRPayCloseRequest.setVersion("6.0"); /* 版本号 */
				
				oneQRPayCloseRequest.setInputCharset("UTF-8"); /* 字符集 (固定为UTF-8)*/
				
				oneQRPayCloseRequest.setSignType("MD5"); /* 加密方式(固定为MD5) */
				
				oneQRPayCloseRequest.setCustomerNo(customerNo); /* 商户号 */
				
				oneQRPayCloseRequest.setOrderNo(p_req.rdSeq); /* 商户订单号 */
				
				oneQRPayCloseRequest.setExt1(""); /* 备注1 */
				
				oneQRPayCloseRequest.setExt2(""); /* 备注2 */

				// 组装发送报文
				VbaoChargeSystemHandler vbaoHandler = new VbaoChargeSystemHandler();
				TradeRequestXml tradeRequestXml = vbaoHandler.creatRequestXml(oneQRPayCloseRequest, actItem.saltKey, getCharset(), TradeCodeEnum.ONEQRCODE_INVALID.getCode());
				
				WriteBankLogLn2("微保一码付获取二维码加密前明文:\r\n" + tradeRequestXml.getOriRequestXml());
				WriteBankLogLn2("微保一码付获取二维码请求数据:\r\n" + tradeRequestXml.getRequestXml());
				
				if (tradeRequestXml.getErrMsg() == null) {
					String oneQRPayCloseURL = bankFront.getUrl("oneQRPayClose");
					if (StringHelper.isNullOrEmpty(oneQRPayCloseURL)) {
						retAts.respCode = "-1";
						retAts.respInfo = "[一码付订单关闭地址:oneQRPayClose]内容为空,请检查BankFront.xml相关配置!";
						
						return retAts;
					}
					
					WriteBankLogLn2("微保一码付订单关闭提交地址:" + oneQRPayCloseURL);
					
					// 获取返回报文
					String oneQRPayCloseRespXml = vbaoHandler.http(oneQRPayCloseURL, tradeRequestXml.getRequestXml(), getCharset());
					WriteBankLogLn2("微保一码付订单关闭响应数据:\r\n" + oneQRPayCloseRespXml);
					
					if (StringHelper.hasAnyChar(oneQRPayCloseRespXml)) {
						// 进行解密
						String oneQRPayCloseOriRespXml = Base64vbao.decode(oneQRPayCloseRespXml, getCharset());
						//oneQRPayCloseOriRespXml = new String(oneQRPayCloseOriRespXml.getBytes(), getCharset());
						WriteBankLogLn2("微保一码付订单关闭解密后明文:\r\n" + oneQRPayCloseOriRespXml);
						
						// 进行验签
						boolean flag = vbaoHandler.verifySignature(oneQRPayCloseOriRespXml, actItem.saltKey, getCharset());
						if (flag) {
							String tmpRetStr = URLDecoder.decode(oneQRPayCloseOriRespXml, getCharset());
							
							StringBuilder tmpSbMsg = new StringBuilder();
							XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr, tmpSbMsg);

							if (tmpBkReader != null) {
								String tmpReturnCode = tmpBkReader.getFirstText("/TResponse/ResultInfo/ResultCode");
								if (tmpReturnCode.equals("T")) {
									retAts.respCode = "0";
									retAts.respInfo = tmpBkReader.getFirstText("/TResponse/ResultInfo/Description");
									retAts.rdSeq = tmpBkReader.getFirstText("/TResponse/OrderInfo/OrderNo");
									
									String errorCode = tmpBkReader.getFirstText("/TResponse/ResultInfo/ErrCode");
									if ("00000".equals(errorCode)) {
										retAts.closeState = CloseOrderState.Close_Success;
									} else {
										retAts.closeState = CloseOrderState.Close_Fail;
										retAts.errorCode = errorCode;
										retAts.errorMsg = tmpBkReader.getFirstText("/TResponse/ResultInfo/Description");
									}
								} else {
									retAts.respCode = "-1";
									retAts.respInfo = tmpBkReader.getFirstText("/TResponse/ResultInfo/Description");
								}
							} else {
								retAts.respCode = "-1";
								retAts.respInfo = tmpSbMsg.toString();
							}
						} else {
							retAts.respCode = "-1";
							retAts.respInfo = "微保一码付订单关闭本地验签失败!";
						}
					}
				} else {
					retAts.respCode = "-1";
					retAts.respInfo = tradeRequestXml.getErrMsg();
				}
			} else {
				retAts.respCode = "-1";
				retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		return retAts;
	}
}
